package web;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Accion;
import model.Precio;
import model.Regla;
import service.ServiceProvider;

//ABM de Reglas

public class PrecioServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		String idStr = req.getParameter("delid");
		
		/*
		if (idStr != null){
			ServiceProvider.getDao().borrarRegla(Integer.parseInt(idStr));
		}
		*/
		
		mostrarRespuesta(req, resp);
	}
	
	private void mostrarRespuesta(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		List<Accion> acciones = ServiceProvider.getDao().getAcciones();
		Collections.sort(acciones, new Comparator<Accion>() {
			@Override
			public int compare(Accion o1, Accion o2) {
				return o1.getRic().compareTo(o2.getRic());
			}
		});
		
		String ric = req.getParameter("ric");
		List<Precio> precios = null;
		
		if (ric != null){
			Accion a = ServiceProvider.getDao().getAccionByRic(ric);
			precios = a.getPrecios();
			Collections.sort(precios, new Comparator<Precio>() {

				@Override
				public int compare(Precio o1, Precio o2) {
					return o2.getFecha().compareTo(o1.getFecha());
				}
			});
		}
		
        PrintWriter out = resp.getWriter();		
        out.println("<HEAD><TITLE>Precios</TITLE></HEAD><BODY>");
        out.println("<a href=\"/tpfinal\">Menu</a>");
        out.println("<form action=\"PrecioServlet\" method=\"POST\">");
        out.println("Accion: ");
        out.println("<select name=ric>" );
        for (Accion a : acciones){
            out.println("<option name=" + a.getRic() + " value=" + a.getRic() + ">" + a.getRic() + "</option>");
        }
        out.println("<input type=\"Submit\" value=\"Ver\" >" );        
        out.println("</form>");
        
        if (precios != null){
    		out.println("<table>");
        	out.println("<tr><td>FECHA</td><td>PRECIO</td></tr>");        	
        	SimpleDateFormat fmt = new SimpleDateFormat("dd/MM/yyyy");
    		
            for (Precio p : precios){
            	out.println("<tr>");        	
                out.println("<td>" + fmt.format(p.getFecha()) + "</td>");
                out.println("<td>" + p.getValor().toString() + "</td>");
                out.println("</tr>");        	
            }
    		out.println("</table>");
        }
        
        out.println("</BODY>");
        out.close();		
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String ant = req.getParameter("ant");
		String cons = req.getParameter("cons");
		
		if (ant != null && ant.trim().length() > 0){
			if (cons != null && cons.trim().length() > 0){
				Regla r = new Regla();
				r.ant = ant;
				r.cons = cons;
				ServiceProvider.getDao().saveRegla(r);
			}
		}
		
		mostrarRespuesta(req, resp);
	}

}
